package cn.com.elite;

import java.io.PrintWriter;
import java.io.StringWriter;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;



public class mailservlet extends HttpServlet{
	private static final Log log=LogFactory.getLog(mailservlet.class);
	private Mailrun mailrun; 
	private Delete_tempfile delete_temp;
	private Thread t; 
	private Thread t1; 
	public void destroy() {
		 
	}
	public static String getERRO(Exception e){
		String str="";
		try{
            StringWriter sw = new StringWriter();  
            e.printStackTrace(new PrintWriter(sw, true));  
            str = sw.toString(); 	
		}catch(Exception e1){
			str=e.toString();
			log.error("getERRO:"+e1.toString());
		}
		return str;
	}	
	public void init(){
        ServletContext application;   
        WebApplicationContext wac;   
        application=getServletContext();   
        wac=WebApplicationContextUtils.getWebApplicationContext(application);//获取spring的context  
        cn.com.elite.MailBean mailbean=(cn.com.elite.MailBean)wac.getBean("mailbean");  
	    mailrun=new Mailrun();
	    delete_temp=new Delete_tempfile();
	    delete_temp.setEml_path(this.getInitParameter("eml_path"));
	    mailrun.setMailbean(mailbean);
	    mailrun.setAddress(this.getInitParameter("address"));
	    mailrun.setSMTPHost(this.getInitParameter("SMTPHost"));
	    mailrun.setUsername(this.getInitParameter("username"));
	    String des=this.getInitParameter("des");
	    String password=this.getInitParameter("password");
	    log.info("des:"+des);
	    log.info("password:"+password);
	    if(des.equals("1")){
	    	DESUtil des1=new DESUtil("1234567");  
	    	String deStr=des1.decryptStr(password); 
	    	 log.info("deStr:"+deStr);
	    	mailrun.setPassword(deStr);
	    }else{
	    	mailrun.setPassword(password);
	    }
	    mailrun.setMail_sql(this.getInitParameter("mailsql"));
	    mailrun.setEml_path(this.getInitParameter("eml_path"));
	    String f_path=this.getInitParameter("f_path");
	    if(f_path!=null&&f_path.length()>0&&!f_path.equals("")){
	    	mailrun.setF_path(this.getInitParameter("f_path"));
	    }else{
	    	try {
				String path=mailbean.chaxun1(" select parametervalue from sysc_parameter where  parameter_id='MAPATH' ");
				mailrun.setF_path(path);
	    	} catch (Exception e) {
	    		log.error("F_path获取失败:"+getERRO(e));
			}
	    }
	    mailrun.setDotime(this.getInitParameter("dotime"));
	    mailrun.setHttp(this.getInitParameter("http"));
	    t=new Thread(mailrun); 
		t.start();
		t1=new Thread(delete_temp); 
		t1.start();
		
	}
}
